#include "EpilLiteralExpressionCode.h"


EpilLiteralExpressionCode::EpilLiteralExpressionCode( const EpilToken & token )
{
	switch( token.type )
	{
	case ETT_Identifier:
		m_type = EpilLiteralExpressionCode::Identifier;
		m_stringData = token.text;
		break;
	case ETT_String:
		m_type = EpilLiteralExpressionCode::String;
		m_stringData = token.text;
		break;
	case ETT_Decimal:
		if( token.text.find( t('.') ) == tstring::npos )
		{
			m_type = EpilLiteralExpressionCode::Integer;
			tstringstream sstream;
			sstream << token.text;
			sstream >> m_intData;
		}
		else
		{
			m_type = EpilLiteralExpressionCode::Float;
			tstringstream sstream;
			sstream << token.text;
			sstream >> m_floatData;
		}
		break;
	default:
		throw runtime_error( "Should be identifier, string or decimal here." );
	}
}


EpilLiteralExpressionCode::~EpilLiteralExpressionCode()
{
}


EpilLiteralExpressionCode::operator int() const
{
	if( m_type == EpilLiteralExpressionCode::Integer )
	{
		return m_intData;
	}

	throw runtime_error( "EpilLiteralExpression is not a integer." );
}


EpilLiteralExpressionCode::operator float() const
{
	if( m_type == EpilLiteralExpressionCode::Float )
	{
		return m_floatData;
	}

	throw runtime_error( "EpilLiteralExpression is not a float." );
}


EpilLiteralExpressionCode::operator tstring() const
{
	if( m_type == EpilLiteralExpressionCode::String )
	{
		return m_stringData;
	}

	throw runtime_error( "EpilLiteralExpression is not a string." );
}